package com.probox.common.entity.service.centre.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.probox.common.entity.domain.centre.ProductCount;
import com.probox.common.entity.mapper.centre.CentreProductCountMapper;
import com.probox.common.entity.service.centre.IProductCountService;
import com.probox.common.entity.vo.centre.WarningDataVo;
import com.probox.common.entity.vo.centre.customer.CustomerQueryVo;
import com.probox.common.entity.vo.centre.map.Legend;
import com.probox.common.entity.vo.centre.map.ProductAxisMap;
import com.probox.common.entity.vo.centre.map.Series;
import com.probox.common.entity.vo.centre.map.XAxis;
import com.probox.common.entity.vo.centre.order.OrderQueryVo;
import com.probox.common.entity.vo.centre.product.ProductDataVo;
import com.probox.common.entity.vo.centre.product.ProductInfoQuery;
import com.probox.common.entity.vo.centre.product.SelectProductInfo;
import com.probox.common.core.enums.ProductOptType;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.core.utils.money.MoneyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 商品数据汇总Service业务层处理
 *
 * @date 2021-06-08
 */
@Service
@Transactional
public class CentreProductCountServiceImpl extends ServiceImpl<CentreProductCountMapper, ProductCount> implements IProductCountService {
    @Autowired
    private CentreProductCountMapper centreProductCountMapper;


    /**
     * 查询商品数据汇总
     *
     * @param countUid 商品数据汇总ID
     * @return 商品数据汇总
     */
    @Override
    public ProductCount selectProductCountById(Long countUid) {
        return this.getById(countUid);
    }

    /**
     * 查询商品数据汇总列表
     *
     * @param productCount 商品数据汇总
     * @return 商品数据汇总
     */
    @Override
    public List<ProductCount> queryList(ProductCount productCount) {
        QueryWrapper<ProductCount> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(productCount.getCountUid())) && productCount.getCountUid() != null) {
            queryWrapper.eq("COUNT_UID", productCount.getCountUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(productCount.getProductUid())) && productCount.getProductUid() != null) {
            queryWrapper.eq("PRODUCT_UID", productCount.getProductUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(productCount.getViewNum())) && productCount.getViewNum() != null) {
            queryWrapper.eq("VIEW_NUM", productCount.getViewNum());
        }
        if (StringUtils.isNotEmpty(String.valueOf(productCount.getCollectNum())) && productCount.getCollectNum() != null) {
            queryWrapper.eq("COLLECT_NUM", productCount.getCollectNum());
        }
        if (StringUtils.isNotEmpty(String.valueOf(productCount.getCommentNum())) && productCount.getCommentNum() != null) {
            queryWrapper.eq("COMMENT_NUM", productCount.getCommentNum());
        }
        if (StringUtils.isNotEmpty(String.valueOf(productCount.getBuyNum())) && productCount.getBuyNum() != null) {
            queryWrapper.eq("BUY_NUM", productCount.getBuyNum());
        }
        if (StringUtils.isNotEmpty(String.valueOf(productCount.getShareNum())) && productCount.getShareNum() != null) {
            queryWrapper.eq("SHARE_NUM", productCount.getShareNum());
        }
        if (StringUtils.isNotEmpty(String.valueOf(productCount.getAddCarNum())) && productCount.getAddCarNum() != null) {
            queryWrapper.eq("ADD_CAR_NUM", productCount.getAddCarNum());
        }
        if (StringUtils.isNotEmpty(String.valueOf(productCount.getCancleNum())) && productCount.getCancleNum() != null) {
            queryWrapper.eq("CANCLE_NUM", productCount.getCancleNum());
        }
        if (StringUtils.isNotEmpty(String.valueOf(productCount.getRefundNum())) && productCount.getRefundNum() != null) {
            queryWrapper.eq("REFUND_NUM", productCount.getRefundNum());
        }
        if (StringUtils.isNotEmpty(String.valueOf(productCount.getBarterNum())) && productCount.getBarterNum() != null) {
            queryWrapper.eq("BARTER_NUM", productCount.getBarterNum());
        }
        return centreProductCountMapper.selectList(queryWrapper);
    }

    /**
     * 新增商品数据汇总
     *
     * @param productCount 商品数据汇总
     * @return 结果
     */
    @Override
    public boolean insertProductCount(ProductCount productCount) {

        return this.save(productCount);

    }

    /**
     * 修改商品数据汇总
     *
     * @param productCount 商品数据汇总
     * @return 结果
     */
    @Override
    public boolean updateProductCount(ProductCount productCount) {

        return this.updateById(productCount);
    }

    /**
     * 批量删除商品数据汇总
     *
     * @param countUids 需要删除的商品数据汇总ID
     * @return 结果
     */
    @Override
    public boolean deleteProductCountByIds(Long[] countUids) {
        return this.removeByIds(Arrays.asList(countUids));
    }

    /**----------------------------------------------------------------------------------------------------**/
    /**
     * 查询商品数据汇总
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public ProductDataVo selectProductData(Date startTime, Date endTime) {

        Long platformId = SecurityUtils.getPlatformId();
        Long supplierId = SecurityUtils.getSupplierId();
        ProductDataVo productDataVo = new ProductDataVo(0, 0, 0, 0, 0, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00);
        if (startTime == null) {
            //查询商品数据
            productDataVo = centreProductCountMapper.selectProductData(platformId, supplierId);
            //总访客数
            productDataVo.setVisitorsSum(centreProductCountMapper.selectProductVisitorsData(platformId, null, null));
            //复购率
            productDataVo.setRepurchaseRateSum(mul(centreProductCountMapper.selectProductRepurchaseRateSum(platformId, null, null), 100.00));
            //转换率
            // productDataVo.setPaymentConversionRateSum( mul(productCountMapper.selectPaymentConversionRateSum(platformId,null,null),100.00));
            productDataVo.setPaymentConversionRateSum(mul(div(productDataVo.getBuyNumSum(), productDataVo.getViewNumSum(), 2), 100.00));
        } else {
            String startTimes = DateUtil.formatDateTime(DateUtil.beginOfDay(startTime));
            String endTimes = DateUtil.formatDateTime(endTime);
            //获取某时间段数据
            productDataVo = getFormerlyData(startTimes, endTimes);
            //某时间段访客数
            productDataVo.setVisitorsSum(centreProductCountMapper.selectProductVisitorsData(platformId, startTimes, endTimes));
            //复购率
            productDataVo.setRepurchaseRateSum(mul(centreProductCountMapper.selectProductRepurchaseRateSum(platformId, startTimes, endTimes), 100.00));
            //转换率
            productDataVo.setPaymentConversionRateSum(mul(centreProductCountMapper.selectPaymentConversionRateSum(platformId, startTimes, endTimes), 100.00));

            //获取时间差
            Integer betweenDay = Integer.parseInt(DateUtil.between(startTime, endTime, DateUnit.DAY) + "") * -1;
            //往前推 7 、30 、
            String startTimesFormerly = DateUtil.formatDateTime(DateUtil.offsetDay(startTime, betweenDay));
            String endTimesFormerly = DateUtil.formatDateTime(DateUtil.offsetDay(endTime, betweenDay));
            //获取某时间段数据
            ProductDataVo productDataVoFormerly = getFormerlyData(startTimesFormerly, endTimesFormerly);
            //获取客户某时间段数量
            productDataVoFormerly.setVisitorsSum(centreProductCountMapper.selectProductVisitorsData(platformId, startTimesFormerly, endTimesFormerly));
            //复购率
            productDataVoFormerly.setRepurchaseRateSum(mul(centreProductCountMapper.selectProductRepurchaseRateSum(platformId, startTimesFormerly, endTimesFormerly), 100.00));
            //转换率
            productDataVoFormerly.setPaymentConversionRateSum(mul(centreProductCountMapper.selectPaymentConversionRateSum(platformId, startTimesFormerly, endTimesFormerly), 100.00));
            //计算上升/下降百分比
            productDataVo = countFormerly(productDataVo, productDataVoFormerly);
        }

        return productDataVo;
    }


    /**
     * 查询当天数据汇总 并返回比较数据
     *
     * @return
     */
    @Override
    public ProductDataVo selectTodayProductData() {
        Long platformId = SecurityUtils.getPlatformId();
        Long supplierId = SecurityUtils.getSupplierId();

        String startTime = DateUtil.formatDateTime(DateUtil.beginOfDay(DateUtil.date()));
        String endTime = DateUtil.formatDateTime(DateUtil.endOfDay(DateUtil.date()));

        List<HashMap> hashMaps = centreProductCountMapper.selectTodayProductData(platformId, startTime, endTime);
        ProductDataVo productDataVo = new ProductDataVo(0, 0, 0, 0, 0, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00);
        hashMaps.stream().forEach(o -> {
            if (ProductOptType.view.getKey().equals(o.get("type"))) {
                productDataVo.setViewNumSum(Integer.parseInt(o.get("total").toString()));
            }
            if (ProductOptType.buy.getKey().equals(o.get("type"))) {
                productDataVo.setBuyNumSum(Integer.parseInt(o.get("total").toString()));
            }
            if (ProductOptType.collect.getKey().equals(o.get("type"))) {
                productDataVo.setCollectNumSum(Integer.parseInt(o.get("total").toString()));
            }
            if (ProductOptType.addCar.getKey().equals(o.get("type"))) {
                productDataVo.setAddCarNumSum(Integer.parseInt(o.get("total").toString()));
            }
        });
        //今日访客数
        productDataVo.setVisitorsSum(centreProductCountMapper.selectProductVisitorsData(platformId, startTime, endTime));
        //复购率
        productDataVo.setRepurchaseRateSum(mul(centreProductCountMapper.selectProductRepurchaseRateSum(platformId, startTime, endTime), 100.00));
        //转换率
        productDataVo.setPaymentConversionRateSum(mul(centreProductCountMapper.selectPaymentConversionRateSum(platformId, startTime, endTime), 100.00));

        String yesterdayStart = DateUtil.formatDateTime(DateUtil.beginOfDay(DateUtil.yesterday()));
        String yesterdayEnd = DateUtil.formatDateTime(DateUtil.endOfDay(DateUtil.yesterday()));

        List<HashMap> yesterday = centreProductCountMapper.selectTodayProductData(platformId, yesterdayStart, yesterdayEnd);
        yesterday.stream().forEach(o -> {
            if (ProductOptType.view.getKey().equals(o.get("type"))) {
                productDataVo.setViewNumFormerly(Double.valueOf(Integer.parseInt(o.get("total").toString())));
            }
            if (ProductOptType.buy.getKey().equals(o.get("type"))) {
                productDataVo.setBuyNumFormerly(Double.valueOf(Integer.parseInt(o.get("total").toString())));
            }
            if (ProductOptType.collect.getKey().equals(o.get("type"))) {
                productDataVo.setCollectNumFormerly(Double.valueOf(Integer.parseInt(o.get("total").toString())));
            }
            if (ProductOptType.addCar.getKey().equals(o.get("type"))) {
                productDataVo.setAddCarNumFormerly(Double.valueOf(Integer.parseInt(o.get("total").toString())));
            }
        });

        //昨日总访客数
        productDataVo.setVisitorsFormerly(Double.valueOf(centreProductCountMapper.selectProductVisitorsData(platformId, yesterdayStart, yesterdayEnd)));
        //昨日复购率
        productDataVo.setRepurchaseRateSumFormerly(mul(centreProductCountMapper.selectProductRepurchaseRateSum(platformId, yesterdayStart, yesterdayEnd), 100.00));
        //昨日转换率
        productDataVo.setPaymentConversionRateFormerly(mul(centreProductCountMapper.selectPaymentConversionRateSum(platformId, yesterdayStart, yesterdayEnd), 100.00));
        return productDataVo;
    }


    /**
     * 获取商品近三十天统计图
     *
     * @return
     */
    @Override
    public ProductAxisMap getProductDataMap(String type) {
        Long platformId = SecurityUtils.getPlatformId();
        Date startTime = new Date();
        Date endTime = DateUtil.lastMonth();
        startTime = DateUtil.endOfDay(startTime);
        endTime = DateUtil.beginOfDay(endTime);

        //生成近三十天日期
        List<String> date = getDate(DateUtil.offsetDay(startTime, -1), endTime);
        XAxis xAxis = new XAxis();
        xAxis.setData(date);

        // 当前时间
        String startTimes = DateUtil.formatDateTime(endTime);
        //一个月前时间
        String formatDateTime = DateUtil.formatDateTime(DateUtil.offsetDay(startTime, -1));

        //获取统计数据
        List<HashMap> list = getMapData(type, platformId, startTimes, formatDateTime);
        List<Double> collect = seriesData(list, date);

        List seriesList = new ArrayList();

        Series series = buildSeriesMap(collect, type);
        seriesList.add(series);

        Legend legend = buildLegendMap(seriesList);
        return buildProductAxisMap(type, seriesList, xAxis, legend);
    }


    /**
     * 获取复购率近三十天统计图
     *
     * @return
     */
    @Override
    public ProductAxisMap getProductDataRepurchaseRateMap(String type) {
        Long platformId = SecurityUtils.getPlatformId();

        Date startTime = new Date();
        Date endTime = DateUtil.lastMonth();
        startTime = DateUtil.endOfDay(startTime);
        endTime = DateUtil.beginOfDay(endTime);
        startTime = DateUtil.endOfDay(startTime);
        endTime = DateUtil.beginOfDay(endTime);


        //生成近三十天日期
        List<String> date = getDate(DateUtil.offsetDay(startTime, -1), endTime);
        XAxis xAxis = new XAxis();
        xAxis.setData(date);

        // 当前时间
        String startTimes = DateUtil.formatDateTime(endTime);
        //一个月前时间
        String formatDateTime = DateUtil.formatDateTime(DateUtil.offsetDay(startTime, -1));

        List seriesList = new ArrayList();
        //获取总支付人数
        List<HashMap> payNumber = centreProductCountMapper.selectPayClient(platformId, startTimes, formatDateTime);

        //获取复购人数
        List<HashMap> repetitionPayNumber = centreProductCountMapper.selectProductRepurchaseClient(platformId, startTimes, formatDateTime);

        List<HashMap> hashMaps = new ArrayList<>();
        for (int i = 0; i < repetitionPayNumber.size(); i++) {
            HashMap hashMap = new HashMap();
            Double repetitionPayTotal = Double.valueOf(repetitionPayNumber.get(i).get("total").toString());
            for (int j = 0; j < payNumber.size(); j++) {
                if (repetitionPayNumber.get(i).get("date").equals(payNumber.get(j).get("date"))) {
                    Double PayTota = new Double(payNumber.get(j).get("total").toString());
                    Double div = div(repetitionPayTotal, PayTota, 2);
                    hashMap.put("date", repetitionPayNumber.get(i).get("date"));
                    hashMap.put("total", mul(div, 100));
                    hashMaps.add(hashMap);
                }
            }
        }
        List<Double> doubles = seriesData(hashMaps, date);

        Series series = buildSeriesMap(doubles, ProductOptType.repurchaseRate.getKey());
        seriesList.add(series);

        Legend legend = buildLegendMap(seriesList);

        return buildProductAxisMap(type, seriesList, xAxis, legend);
    }


    /**
     * 查询范围内支付转化率
     *
     * @param type
     * @return
     */
    @Override
    public ProductAxisMap getPaymentConversionRateSumMap(String type) {
        Long platformId = SecurityUtils.getPlatformId();
        Long supplierId = SecurityUtils.getSupplierId();

        Date startTime = new Date();
        Date endTime = DateUtil.lastMonth();
        startTime = DateUtil.endOfDay(startTime);
        endTime = DateUtil.beginOfDay(endTime);

        //生成近三十天日期
        List<String> date = getDate(DateUtil.offsetDay(startTime, -1), endTime);
        XAxis xAxis = new XAxis();
        xAxis.setData(date);

        // 当前时间
        String startTimes = DateUtil.formatDateTime(endTime);
        //一个月前时间
        String formatDateTime = DateUtil.formatDateTime(DateUtil.offsetDay(startTime, -1));

        //获取统计数据
        List<HashMap> list = centreProductCountMapper.getPaymentConversionRateSum(platformId, startTimes, formatDateTime);
        List<Double> collect = seriesData(list, date, 100);

        List seriesList = new ArrayList();

        Series series = buildSeriesMap(collect, type);
        seriesList.add(series);

        Legend legend = buildLegendMap(seriesList);
        return buildProductAxisMap(type, seriesList, xAxis, legend);
    }


    /**
     * 查询商品详细数据汇总
     *
     * @param productInfo
     * @return
     */
    @Override
    public List<ProductDataVo> productQueryList(ProductInfoQuery productInfo) {
        Long platformId = SecurityUtils.getPlatformId();

        if (productInfo.getStartTime() != null) {
            productInfo.setStartTime(DateUtil.beginOfDay(productInfo.getStartTime()));
        } else if (productInfo.getEndTime() != null) {
            productInfo.setEndTime(DateUtil.beginOfDay(productInfo.getEndTime()));
        }
        return  centreProductCountMapper.selectProductQueryList(productInfo, platformId);
    }

    /**
     * 查询前十商品浏览量
     *
     * @param productInfo
     * @return
     */
    @Override
    public List<ProductDataVo> getRanking(ProductInfoQuery productInfo) {
        Long platformId = SecurityUtils.getPlatformId();

        SelectProductInfo selectProductInfo = new SelectProductInfo();
        BeanUtils.copyProperties(productInfo, selectProductInfo);

        //昨日开始时间
        Date yesterdayStart = DateUtil.beginOfDay(DateUtil.yesterday());
        //昨日结束时间
        Date yesterdayEnd = DateUtil.endOfDay(DateUtil.yesterday());

        DateTime dateTime = DateUtil.offsetDay(DateUtil.lastWeek(), -1);
        //七天前开始时间
        String lastWeekStart = DateUtil.formatDateTime(DateUtil.beginOfDay(dateTime));
        //七天前结束时间
        String lastWeekEnd = DateUtil.formatDateTime(DateUtil.endOfDay(dateTime));

        selectProductInfo.setStartTime(yesterdayStart);
        selectProductInfo.setEndTime(yesterdayEnd);

        selectProductInfo.setStartTimeFormerly(lastWeekStart);
        selectProductInfo.setEndTimeFormerly(lastWeekEnd);


        return  centreProductCountMapper.selectRanking(selectProductInfo, platformId);
    }

    /**
     * 查询商品预计数据
     *
     * @param orderQueryVo
     * @return
     */
    @Override
    public WarningDataVo getProductWarningData(OrderQueryVo orderQueryVo) {
        WarningDataVo vo = new WarningDataVo();
        vo.setProductInventoryTotal(centreProductCountMapper.selectProductWarningData(orderQueryVo));
        vo.setProductTotal(centreProductCountMapper.selectProductTotal(SecurityUtils.getPlatformId()));
        return vo;
    }

    /**
     * 访客数
     *
     * @return
     */
    @Override
    public Integer selectVisitorsTotal(CustomerQueryVo customerQueryVo) {
        String begTime = null;
        String endTime = null;
        if (customerQueryVo.getStartTime() != null) {
            begTime = DateUtil.formatDate(DateUtil.beginOfDay(customerQueryVo.getStartTime()));
        }
        if (customerQueryVo.getEndTime() != null) {
            endTime = DateUtil.formatDate(DateUtil.endOfDay(customerQueryVo.getEndTime()));
        }
        return centreProductCountMapper.selectProductVisitorsData(SecurityUtils.getPlatformId(), begTime, endTime);
    }


    /**
     * 获取图表数据
     *
     * @param type       类型
     * @param platformId 平台Id
     * @param startTime  开始时间
     * @param endTime    结束时间
     * @return
     */
    public List<HashMap> getMapData(String type, Long platformId, String startTime, String endTime) {
        List<HashMap> list = new ArrayList<>();
        if (ProductOptType.visitors.getKey().equals(type)) {
            //获取访客数据
            list = centreProductCountMapper.selectVisitorsMapData(platformId, startTime, endTime);
        } else {
            //获取浏览量/支付数/购物车数/收藏数据
            list = centreProductCountMapper.selectMapData(type, platformId, startTime, endTime);
        }

        return list;

    }


    public ProductDataVo getFormerlyData(String startTime, String endTime) {
        Long platformId = SecurityUtils.getPlatformId();
        Long supplierId = SecurityUtils.getSupplierId();
        //按范围查询
        List<HashMap> hashMaps = centreProductCountMapper.selectTodayProductData(platformId, startTime, endTime);
        ProductDataVo productDataVo = new ProductDataVo(0, 0, 0, 0, 0, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00);
        hashMaps.stream().forEach(o -> {
            if (ProductOptType.view.getKey().equals(o.get("type"))) {
                productDataVo.setViewNumSum(Integer.parseInt(o.get("total").toString()));
            }
            if (ProductOptType.buy.getKey().equals(o.get("type"))) {
                productDataVo.setBuyNumSum(Integer.parseInt(o.get("total").toString()));
            }
            if (ProductOptType.collect.getKey().equals(o.get("type"))) {
                productDataVo.setCollectNumSum(Integer.parseInt(o.get("total").toString()));
            }
            if (ProductOptType.addCar.getKey().equals(o.get("type"))) {
                productDataVo.setAddCarNumSum(Integer.parseInt(o.get("total").toString()));
            }
        });
        return productDataVo;
    }

    ;


    /**
     * 当前时间段数据：过去时间段数据比较
     *
     * @param productDataVo
     * @param formerlyDataVo
     * @return
     */
    public ProductDataVo countFormerly(ProductDataVo productDataVo, ProductDataVo formerlyDataVo) {

        productDataVo.setViewNumFormerly(difference(productDataVo.getViewNumSum(), formerlyDataVo.getViewNumSum(), 2));

        productDataVo.setCollectNumFormerly(difference(productDataVo.getCollectNumSum(), formerlyDataVo.getCollectNumSum(), 2));

        productDataVo.setBuyNumFormerly(difference(productDataVo.getBuyNumSum(), formerlyDataVo.getBuyNumSum(), 2));

        productDataVo.setAddCarNumFormerly(difference(productDataVo.getAddCarNumSum(), formerlyDataVo.getAddCarNumSum(), 2));

        productDataVo.setRepurchaseRateSumFormerly(difference(productDataVo.getRepurchaseRateSum(), formerlyDataVo.getRepurchaseRateSum(), 2));

        productDataVo.setPaymentConversionRateFormerly(difference(productDataVo.getPaymentConversionRateSum(), formerlyDataVo.getPaymentConversionRateSum(), 2));

        return productDataVo;
    }

    ;

    /**
     * 构建buildSeries对象
     *
     * @param collect
     * @param type
     * @return
     */
    public Series buildSeriesMap(List<Double> collect, String type) {
        Series series = new Series();
        series.setData(collect);
        series.setType("line");
//        series.setStack("总量");
        if (ProductOptType.view.getKey().equals(type)) {
            series.setName(ProductOptType.view.getValue());
            series.setColor("#4169E1");
        } else if (ProductOptType.collect.getKey().equals(type)) {
            series.setName(ProductOptType.collect.getValue());
            series.setColor("#FFFF00");
        } else if (ProductOptType.buy.getKey().equals(type)) {
            series.setName(ProductOptType.buy.getValue());
        } else if (ProductOptType.addCar.getKey().equals(type)) {
            series.setName(ProductOptType.addCar.getValue());
        } else if (ProductOptType.repurchaseRate.getKey().equals(type)) {
            series.setName(ProductOptType.repurchaseRate.getValue());
        } else if (ProductOptType.paymentConversionRate.getKey().equals(type)) {
            series.setName(ProductOptType.paymentConversionRate.getValue());
        } else if (ProductOptType.repurchaseClient.getKey().equals(type)) {
            series.setName(ProductOptType.repurchaseClient.getValue());
        } else if (ProductOptType.visitors.getKey().equals(type)) {
            series.setName(ProductOptType.visitors.getValue());
        }
        return series;
    }

    /**
     * 构建buildLegendMap对象
     *
     * @return
     */
    public Legend buildLegendMap(List<Series> list) {
        Legend legend = new Legend();
        List datalist = new ArrayList<>();
        for (Series i : list) {
            datalist.add(i.getName());
        }
        legend.setData(datalist);
        return legend;
    }

    /**
     * 构建 ProductAxisMap 对象
     *
     * @return
     */
    public ProductAxisMap buildProductAxisMap(String type, List<Series> seriesList, XAxis xAxis, Legend legend) {
        ProductAxisMap productAxisMap = new ProductAxisMap();
        if (ProductOptType.view.getKey().equals(type)) {
            productAxisMap.setTitle("商品浏览量折线图");
        } else if (ProductOptType.collect.getKey().equals(type)) {
            productAxisMap.setTitle("商品收藏量折线图");
        } else if (ProductOptType.buy.getKey().equals(type)) {
            productAxisMap.setTitle("商品销售量折线图");
        } else if (ProductOptType.addCar.getKey().equals(type)) {
            productAxisMap.setTitle("加入购物车折线图");
        } else if (ProductOptType.repurchaseRate.getKey().equals(type)) {
            productAxisMap.setTitle("复购率折线图");
        } else if (ProductOptType.paymentConversionRate.getKey().equals(type)) {
            productAxisMap.setTitle("支付转化率折线图");
        } else {
            productAxisMap.setTitle("访客量折线图");
        }
        productAxisMap.setXAxis(xAxis);
        productAxisMap.setSeries(seriesList);
        productAxisMap.setLegend(legend);
        return productAxisMap;
    }

    /**
     * 构建 seriesData 下的数据
     *
     * @param list
     * @param date
     * @return
     */
    public List<Double> seriesData(List<HashMap> list, List<String> date) {
        List<Double> collect = date.stream().map(o -> {
            Double total = 0.00;
            HashMap hashMap = new HashMap();
            hashMap = list.stream().filter(i -> i.get("date").equals(o)).findAny().orElse(null);
            if (hashMap != null) {
                total = Double.valueOf(hashMap.get("total").toString());
            }
            return total;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 构建 seriesData 下的数据
     *
     * @param list
     * @param date
     * @return
     */
    public List<Double> seriesData(List<HashMap> list, List<String> date, Integer percentage) {
        List<Double> collect = date.stream().map(o -> {
            Double total = 0.00;
            HashMap hashMap = new HashMap();
            hashMap = list.stream().filter(i -> i.get("date").equals(o)).findAny().orElse(null);
            if (hashMap != null) {
                total = mul(Double.valueOf(hashMap.get("total").toString()), percentage);
            }
            return total;
        }).collect(Collectors.toList());
        return collect;
    }


    /**
     * 提供精确的乘法运算
     *
     * @param v1 被乘数
     * @param v2 乘数
     * @return 两个参数的积
     */
    public static double mul(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.multiply(b2).doubleValue();
    }

    /**
     * 提供（相对）精确的除法运算。当发生除不尽的情况时，由scale参数指
     * 定精度，以后的数字四舍五入
     *
     * @param v1    被除数
     * @param v2    除数
     * @param scale 表示需要精确到小数点以后几位
     * @return 两个参数的商
     */
    public static Double div(Integer v1, Integer v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        if (v2 == 0) {
            return Double.valueOf(v1);
        }
        return Double.valueOf(b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).toString());
    }


    public static Double difference(Integer v1, Integer v2, Integer scale) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);

        if (b2.compareTo(BigDecimal.ZERO) == 0) {
            return 00.0;
        }
        BigDecimal divide = b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP);
//        String total = divide.multiply(new BigDecimal(100)).toString();
        String total = MoneyUtils.changeY2F(divide.toString());
        if ("0".equals(total)) {
            return 00.0;
        }
        return new Double(total);
    }

    public static Double difference(Double v1, Double v2, Integer scale) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);

        if (b2.compareTo(BigDecimal.ZERO) == 0) {
            return 00.0;
        }
        BigDecimal divide = b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP);
//        String total = divide.multiply(new BigDecimal(100)).toString();
        String total = MoneyUtils.changeY2F(divide.toString());
        if ("0".equals(total)) {
            return 00.0;
        }
        return new Double(total);
    }


    /**
     * 提供（相对）精确的除法运算。当发生除不尽的情况时，由scale参数指
     * 定精度，以后的数字四舍五入
     *
     * @param v1    被除数
     * @param v2    除数
     * @param scale 表示需要精确到小数点以后几位
     * @return 两个参数的商
     */
    public static Double div(Double v1, Double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        if (v2 == 0.00) {
            return v1;
        }
        return Double.valueOf(b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).toString());
    }

    /**
     * 获取某段时间内的日期集合
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static List getDate(Date startTime, Date endTime) {
        List list = new ArrayList<>();
        long betweenDay = DateUtil.between(startTime, endTime, DateUnit.DAY);
        for (int i = 0; i < betweenDay; i++) {
            endTime = DateUtil.offsetDay(endTime, 1);
            String format = DateUtil.format(endTime, "yyyy-MM-dd");
            list.add(format);
        }
        return list;
    }

}
